package com.sc.nft.admin.rpc;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.IdcardInfo;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.DaoLastMonthUserInfoVO;
import com.sc.nft.entity.vo.DaoUserInfoVO;
import com.sc.nft.entity.vo.ObtainWhetherTheUserHasACityMasterCardOrAResidentCardVO;
import com.sc.nft.mapper.IdcardInfoMapper;
import com.sc.nft.mapper.UserInfoMapper;
import com.sc.nft.service.UserInfoService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/userInfo")
public class UserInfoRpc {

    private final UserInfoService userInfoService;
    private final UserCreatorInfoDao userCreatorInfoDao;
    private final UserInfoMapper userInfoMapper;
    private final IdcardInfoMapper idcardInfoMapper;

    @ApiOperation(value = "获取用户是否拥有城主卡或者居民卡", notes = "用户创建群组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/obtainWhetherTheUserHasACityMasterCardOrAResidentCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<ObtainWhetherTheUserHasACityMasterCardOrAResidentCardVO> obtainWhetherTheUserHasACityMasterCardOrAResidentCard(@RequestBody IdRequest idRequest) {
        ObtainWhetherTheUserHasACityMasterCardOrAResidentCardVO obtainWhetherTheUserHasACityMasterCardOrAResidentCardVO = userInfoService.obtainWhetherTheUserHasACityMasterCardOrAResidentCard(idRequest.getId());
        return Result.success(obtainWhetherTheUserHasACityMasterCardOrAResidentCardVO);
    }

    @PostMapping("/getOneById")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<UserInfo> getOneById(@RequestBody IdRequest idRequest){
        return Result.success(userInfoService.getById(idRequest.getId()));
    }

    @PostMapping("/getDaoUserInfo")
    @ApiOperation(value = "获取团队用户信息", notes = "获取团队用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<DaoUserInfoVO> getDaoUserInfo(@RequestBody IdRequest request){
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(request.getId());
        DaoUserInfoVO daoUserInfoVO = new DaoUserInfoVO();
        daoUserInfoVO.setRegisterCount(userInfoMapper.getUnderTeamCount(userCreatorInfo.getUserId()));
        daoUserInfoVO.setRealNameCount(userInfoMapper.getUnderRealNameCount(userCreatorInfo.getUserId()));
        return Result.success(daoUserInfoVO);
    }


    @PostMapping("/getLastMonthUserInfo")
    @ApiOperation(value = "获取一个月内用户信息", notes = "获取一个月内用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<DaoLastMonthUserInfoVO>> getLastMonthUserInfo(@RequestBody IdRequest request){
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(request.getId());
        DateTime now = DateTime.now();
        DateTime findDate = DateUtil.beginOfDay(now.offsetNew(DateField.DAY_OF_MONTH, -31));
        List<DaoLastMonthUserInfoVO> lastMonthUserInfo = userInfoMapper.getLastMonthUserInfo(userCreatorInfo.getUserId(),findDate );
        List<DaoLastMonthUserInfoVO> lastMonthCardInfo = idcardInfoMapper.getLastMonthCardInfo(userCreatorInfo.getUserId(), findDate);
        List<DaoLastMonthUserInfoVO> result = Lists.newArrayList();
        for (int i = 1; i <= 31; i++) {
            DaoLastMonthUserInfoVO daoLastMonthUserInfoVO = new DaoLastMonthUserInfoVO();
            daoLastMonthUserInfoVO.setCreateDate(DateUtil.beginOfDay(now.offsetNew(DateField.DAY_OF_MONTH,-i)));
            daoLastMonthUserInfoVO.setRegisterCount(lastMonthUserInfo.stream().filter(x->x.getCreateDate().equals(daoLastMonthUserInfoVO.getCreateDate())).map(DaoLastMonthUserInfoVO::getRegisterCount).findFirst().orElseGet(()->0));
            daoLastMonthUserInfoVO.setRealNameCount(lastMonthCardInfo.stream().filter(x->x.getCreateDate().equals(daoLastMonthUserInfoVO.getCreateDate())).map(DaoLastMonthUserInfoVO::getRealNameCount).findFirst().orElseGet(()->0));
            result.add(daoLastMonthUserInfoVO);
        }
        return Result.success(result);
    }
}
